#include "car_control.h"

/*
#define struct pwm{
        int pin;
        int value;
        int period;
        int duty_cycle;
}PWM;

#define struct car_cfg_{
        GPIO lw_gpioA;
		GPIO lw_gpioB;
        GPIO rw_gpioA;       
        GPIO rw_gpioB;
        PWM  lw_can;
        PWM  rw_can;
}car_cfg;


#define struct robot_car_ {
        car_cfg cfg;

} robot_car;
*/

int car_init(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_export(cfg->lw_gpioA));
	EXC_RESULT(ret,gpio_export(cfg->lw_gpioB));
	EXC_RESULT(ret,gpio_export(cfg->rw_gpioA));
	EXC_RESULT(ret,gpio_export(cfg->rw_gpioB));
	EXC_RESULT(ret,gpio_direction(cfg->lw_gpioA));
        EXC_RESULT(ret,gpio_direction(cfg->lw_gpioB));
        EXC_RESULT(ret,gpio_direction(cfg->rw_gpioA));
        EXC_RESULT(ret,gpio_direction(cfg->rw_gpioB));
//	EXC_RESULT(ret,pwm_export(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_export(cfg->rw_can->pin));
	return ret;
}

int car_move_on(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_0)); //left move on
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioB,GPIO_VALUE_1));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_0));//right move on
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioB,GPIO_VALUE_1));
//	EXC_RESULT(ret,pwm_enable(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_enable(cfg->rw_can->pin));
	return ret;
}

int car_stop(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_0)); //left stop
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioB,GPIO_VALUE_0));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_0));//right stop
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioB,GPIO_VALUE_0));
//	EXC_RESULT(ret,pwm_enable(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_enable(cfg->rw_can->pin));
	return ret;
}

int car_back_up(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_1)); //left move back
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioB,GPIO_VALUE_0));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_1));//right move back
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioB,GPIO_VALUE_0));
//	EXC_RESULT(ret,pwm_enable(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_enable(cfg->rw_can->pin));
	return ret;
}

int car_turn_left(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_1)); //left move back
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioB,GPIO_VALUE_0));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_0));//right move on
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioB,GPIO_VALUE_1));
//	EXC_RESULT(ret,pwm_enable(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_enable(cfg->rw_can->pin));
	return ret;
}

int car_turn_right(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_0)); //left move on
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioB,GPIO_VALUE_1));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_1));//right move back
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioB,GPIO_VALUE_0));
//	EXC_RESULT(ret,pwm_enable(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_enable(cfg->rw_can->pin));
	return ret;
}

int car_speed_up(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	/*(EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_0)); //left move on
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_1));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_0));//right move on
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_1));*/
//	EXC_RESULT(ret,pwm_config(cfg->lw_can));
//	EXC_RESULT(ret,pwm_config(cfg->rw_can));
	return ret;
}

int car_speed_down(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	/*EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_0)); //left move on
	EXC_RESULT(ret,gpio_write(cfg->lw_gpioA,GPIO_VALUE_1));

	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_0));//right move on
	EXC_RESULT(ret,gpio_write(cfg->rw_gpioA,GPIO_VALUE_1));*/
//	EXC_RESULT(ret,pwm_config(cfg->lw_can));
//	EXC_RESULT(ret,pwm_config(cfg->rw_can));
	return ret;
}

int car_uninit(robot_car* car)
{
	int ret = 0;
	car_cfg* cfg = car->cfg;
	EXC_RESULT(ret,gpio_unexport(cfg->lw_gpioA));
	EXC_RESULT(ret,gpio_unexport(cfg->rw_gpioA));
	EXC_RESULT(ret,gpio_unexport(cfg->lw_gpioB));
	EXC_RESULT(ret,gpio_unexport(cfg->rw_gpioB));
//	EXC_RESULT(ret,pwm_unexport(cfg->lw_can->pin));
//	EXC_RESULT(ret,pwm_unexport(cfg->rw_can->pin));
	return ret;

}
